package com.hurl.waguji.biz.download;

import java.util.List;

import com.hurl.library.spider.UrlPattern;
import com.hurl.library.util.StringUtils;
import com.hurl.waguji.base.Formatter;
import com.hurl.waguji.base.dataprocess.ProcessParams;
import com.hurl.waguji.persistence.DmGpBO;
import com.hurl.waguji.persistence.JcsjCwCwbbThsBO;
import com.hurl.waguji.persistence.JcsjCwCwbbThsBOId;
import com.hurl.waguji.persistence.JcsjCwCwgcThsBO;

class CwCwgcThsProcessor extends BaseDownloadForUpdateProcessor {


	public CwCwgcThsProcessor(ProcessParams params,
			DownloadIdentifier identifier) {
		super(params, identifier);
	}

	@Override
	public void convertHTML2BO(String url, String html, BOSaver collector) {

		if(StringUtils.isBlank(html)){
			return;
		}
		
		String gpdm = context.getEnvAsString("gpdm");
		
		//财务报表
		List<String> cwbb = StringUtils.substringsBetween(html, "<div class=\"cwfx_data\"></div></a>","</td>");
		if(cwbb!=null && !cwbb.isEmpty()){
			for(String a : cwbb){
				String xzdz = StringUtils.substringBetween(a, "href=\"", "\" target=\"_blank\">");
				String mc = StringUtils.substringBetween(a, " target=\"_blank\">", "</a>");
				
				JcsjCwCwbbThsBO bo = new JcsjCwCwbbThsBO();
				bo.setId(new JcsjCwCwbbThsBOId(gpdm,Formatter.toString(mc)));
				bo.setXzdz(Formatter.toString(xzdz));
				bo.setLastupdate(currentTime);
				
				collector.addBO(bo);
			}
		}
		
		//财务构成
		String fz = StringUtils.substringBetween(html, "负债构成", "</table>");
		String zc = StringUtils.substringBetween(html, "资产构成", "</table>");
		if(StringUtils.isNotBlank(fz) || StringUtils.isNotBlank(zc)){
			JcsjCwCwgcThsBO bo = new JcsjCwCwgcThsBO();
			if(StringUtils.isNotBlank(fz)){
				String cqfz = StringUtils.substringBetween(fz, "长期负债</td>", "</td>");
				cqfz = StringUtils.substringBetween(cqfz, "<span class=\"text_right\">", "</span>");
				
				String ldfz = StringUtils.substringBetween(fz, "流动负债</td>", "</td>");
				ldfz = StringUtils.substringBetween(ldfz, "<span class=\"text_right\">", "</span>");
	
				String qtfz = StringUtils.substringBetween(fz, "其它</td>", "</td>");
				qtfz = StringUtils.substringBetween(qtfz, "<span class=\"text_right\">", "</span>");
	
				String fzhj = StringUtils.substringBetween(fz, "负债合计</td>", "</td>");
				fzhj = StringUtils.substringBetween(fzhj, "<span class=\"text_right\">", "</span>");
				
				bo.setGpDm(gpdm);
				bo.setCqfz(Formatter.toDouble(cqfz)*10000);
				bo.setLdfz(Formatter.toDouble(ldfz)*10000);
				bo.setQtfz(Formatter.toDouble(qtfz)*10000);
				bo.setFzhj(Formatter.toDouble(fzhj)*10000);
			}
			if(StringUtils.isNotBlank(zc)){
				String ldzc = StringUtils.substringBetween(zc, "流动资产</td>", "</td>");
				ldzc = StringUtils.substringBetween(ldzc, "<span class=\"text_right\">", "</span>");
				
				String cqtz = StringUtils.substringBetween(zc, "长期投资</td>", "</td>");
				cqtz = StringUtils.substringBetween(cqtz, "<span class=\"text_right\">", "</span>");
	
				String gdzc = StringUtils.substringBetween(zc, "固定资产</td>", "</td>");
				gdzc = StringUtils.substringBetween(gdzc, "<span class=\"text_right\">", "</span>");
	
				String wxzc = StringUtils.substringBetween(zc, "无形资产</td>", "</td>");
				wxzc = StringUtils.substringBetween(wxzc, "<span class=\"text_right\">", "</span>");
				
				String qtzc = StringUtils.substringBetween(zc, "其它</td>", "</td>");
				qtzc = StringUtils.substringBetween(qtzc, "<span class=\"text_right\">", "</span>");
	
				String zchj = StringUtils.substringBetween(zc, "资产合计</td>", "</td>");
				zchj = StringUtils.substringBetween(zchj, "<span class=\"text_right\">", "</span>");
				
				bo.setGpDm(gpdm);
				bo.setLdzc(Formatter.toDouble(ldzc)*10000);
				bo.setCqtz(Formatter.toDouble(cqtz)*10000);
				bo.setGdzc(Formatter.toDouble(gdzc)*10000);
				bo.setWxzc(Formatter.toDouble(wxzc)*10000);
				bo.setQtzc(Formatter.toDouble(qtzc)*10000);
				bo.setZchj(Formatter.toDouble(zchj)*10000);
			}
			bo.setLastupdate(currentTime);
			
			collector.addBO(bo);
		}
	}

	@Override
	public UrlPattern getUrlPattern() {
		return new StockIteratorUrlPattern(context) {
			
			@Override
			public String bo2url(DmGpBO bo) {
				return "http://basic.10jqka.com.cn/"+bo.getGpDmJ()+"/cwfx.html";
			}
		};
	}
}
